﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Ajax
{
    /// <summary>
    ///Good 的摘要说明
    /// </summary>
    public class Good
    {
        public Good()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 根据商品ID，查询出规格、颜色、进货价格（即商品原价） 
        /// </summary>
        [AjaxPro.AjaxMethod]
        public DataTable GetGoodInfo(string goodNo)
        {
            DataTable dt = null;
            DAL.Tables.tbl_Shop_Good table = new DAL.Tables.tbl_Shop_Good();

            try
            {
                dt = table.Open("[ID],[GoodNo],[GoodName_CN],[GoodColor],[GoodNorm],[Price_Vip],[Price_Orig]", "GoodNo = '" + goodNo + "'", "");
            }
            catch { }

            return dt;
        }

        /// <summary>
        /// 根据商品ID，查询出规格、颜色、进货价格（即商品原价） 
        /// </summary>
        [AjaxPro.AjaxMethod]
        public DataTable GetGoodInfoByID(string ID)
        {
            long GoodID = Shove._Convert.StrToLong(ID, 0);
            DataTable dt = null;
            DAL.Tables.tbl_Shop_Good table = new DAL.Tables.tbl_Shop_Good();

            try
            {
                dt = table.Open("[ID],[GoodNo],[GoodName_CN],[GoodColor],[GoodNorm],[Price_Vip],[Price_Orig]", "ID = '" + GoodID + "'", "");
            }
            catch { }

            return dt;
        }

        [AjaxPro.AjaxMethod]
        public DataTable GetGoods(string filename)
        {

            HttpCookie c = HttpContext.Current.Request.Cookies[filename];
            DataTable dt = null;
            if (c != null && c.HasKeys)
            {
                for (int i = 0; i < c.Values.Count; i++)
                {
                    string[] vals = c.Values[i].Split('|');

                    GoodOfCart Good = new GoodOfCart();
                    Good.ID = int.Parse(vals[0]);
                    try
                    {
                        dt = new DAL.Tables.tbl_Shop_Good().Open("", "ID=" + Good.ID.ToString(), "");
                    }
                    catch { }

                }

            }
            return dt;


        }
        /// <summary>
        /// 查询地址
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public DataTable GetAddressByID(string ID)
        {
            long AddressID = Shove._Convert.StrToLong(ID, 0);
            DataTable dt = null;
            DAL.Tables.tbl_Site_ConsigneeAddress table = new DAL.Tables.tbl_Site_ConsigneeAddress();

            try
            {
                dt = table.Open("", "ID = '" + AddressID + "'", "");
            }
            catch { }

            return dt;
        }
        /// <summary>
        /// 查询订单明细
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public DataTable GetSalesByID(string ID)
        {
            long SalesID = Shove._Convert.StrToLong(ID, 0);
            DataTable dt = null;
            DAL.Views.V_Shop_SalesMaster table = new DAL.Views.V_Shop_SalesMaster();
            //SalesNO, CONVERT(varchar(100), CreateDate, 0) as CreateDate,ConsigneeName, ConsigneeTel, ConsigneeMobile, ConsigneeEmail, ConsigneeZipCode, ConsigneeAdress, PayTypeName, CarriageTypeName, ActualMoney
            try
            {
                dt = table.Open("", "ID = '" + SalesID + "'", "");
            }
            catch { }

            return dt;
        }
        /// <summary>
        /// 查询订单商品明细
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public String GetSalesDetailsByID(string ID)
        {
            long SalesID = Shove._Convert.StrToLong(ID, 0);
            DataTable dt = null;
            DAL.Tables.tbl_Shop_SalesDetail table = new DAL.Tables.tbl_Shop_SalesDetail();

            try
            {
                dt = table.Open("GoodName_CN, GoodNorm, Amount, Price_Vip", "SalesID = '" + SalesID + "'", "");
            }
            catch { }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append("<table width=\"500px\">");
            sb.Append("<tr><td width=\"25%\">商品名称</td><td width=\"40%\">商品描述</td><td width=\"15%\">商品数量</td><td width=\"20%\">商品价格</td></tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<tr>");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    sb.Append("<td>");
                    sb.Append(dt.Rows[i][j].ToString());
                    sb.Append("</td>");

                }

                sb.Append("</tr>");
            }

            sb.Append("</table>");

            return sb.ToString();
        }
        /// <summary>
        /// 加载购物车
        /// </summary>
        /// 
        [AjaxPro.AjaxMethod]
        public static string GetCookieItems(string filename)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            HttpCookie c = HttpContext.Current.Request.Cookies[filename];
            double totalMoney = 0;

            if (c != null && c.HasKeys)
            {

                sb.Append("<table width=\"400px\">");
                sb.Append("<tr><td width=\"20%\">商品图片</td><td width=\"20%\">商品名称</td><td width=\"20%\">商品描述</td><td width=\"10%\">商品价格</td><td width=\"10%\">商品数量</td><td width=\"20%\">操作</td></tr>");
                for (int i = 0; i < c.Values.Count; i++)
                {
                    sb.Append("<tr>");
                    string[] vals = c.Values[i].Split('|');

                    GoodOfCart Good = new GoodOfCart();
                    Good.ID = int.Parse(vals[0]);
                    Good.Amount = int.Parse(vals[1]);
                    Good.GoodNorm = Microsoft.JScript.GlobalObject.unescape(vals[2].ToString());
                    Good.GoodColor = Microsoft.JScript.GlobalObject.unescape(vals[3].ToString());

                    DataTable dt = new DAL.Tables.tbl_Shop_Good().Open("", "ID=" + Good.ID.ToString(), "");
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        sb.Append("<td>");
                        sb.Append(Good.GoodName_CN ="<img width=\"80px\" height=\"80px\" src=\"UpFile/Goods/"+ dr["GoodPic_Small"].ToString()+"\" />");
                        sb.Append("</td>");
                        
                        sb.Append("<td>");
                        sb.Append(Good.GoodName_CN = dr["GoodName_CN"].ToString());
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(Good.GoodNorm = dr["GoodNorm"].ToString());
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(Good.Price_Vip = double.Parse(dr["Price_Vip"].ToString()));
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(Good.Amount.ToString());
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append("<input type=\"button\" onclick=\"DeleteGood(" + Good.ID + ")\" id=\"input\" />删除</input>");
                        sb.Append("</td>");
                      
                    }
                sb.Append("</tr>");
                sb.Append("<tr>");
                sb.Append("</tr>");
                }
                sb.Append("</table>");
            }

            return sb.ToString() ;
        }
    }
}